//
//  TDBankSearchViewController.m
//  TFB
//
//  Created by 德古拉丶 on 15/6/16.
//  Copyright (c) 2015年 TD. All rights reserved.
//
#include "BluetoothCell.h"
#import "SHBankInfoModel.h"
#import "TDBankSearchViewController.h"
#import "ChineseInclude.h"
#import "PinYinForObjc.h"
#import "TDBindBankCardViewController.h"
@interface TDBankSearchViewController (){
   
    NSArray * _bankNameList;

}

@end

@implementation TDBankSearchViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"支行查询";
    //[self backButton];
    mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    mySearchBar.delegate = self;
    [mySearchBar setPlaceholder:@"搜索列表"];
    
    searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:mySearchBar contentsController:self];
    searchDisplayController.active = NO;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableHeaderView = mySearchBar;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return searchResults.count;
    }
    else {
        return _dataArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"subBranchCell";
    BluetoothCell *cell = (BluetoothCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[BluetoothCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        
        SHBankInfoModel * info = searchResults[indexPath.row];
        cell.bluetoothLabel.text =  info.subBranch;
        
    }
    else {
        SHBankInfoModel * info = _dataArray[indexPath.row];
        cell.bluetoothLabel.text = info.subBranch;
    }
    cell.bluetoothLabel.numberOfLines = 0;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if ([_delegate respondsToSelector:@selector(getBankNameInfo:)]) {
//        [_delegate getBankNameInfo:_dataArray[indexPath.row]];
//        [self.navigationController popViewControllerAnimated:YES];
//    }

    if (_PopBlock) {
        if (searchResults.count > 0) {
            SHBankInfoModel *info = searchResults[indexPath.row];
            _PopBlock(info.subBranch);
            [Tool setObject:[searchResults[indexPath.row] cnapsCode] forKey:@"JM支行代号"];

        }else{
            SHBankInfoModel *info = _dataArray[indexPath.row];
            _PopBlock(info.subBranch);
            [Tool setObject:[_dataArray[indexPath.row] cnapsCode] forKey:@"JM支行代号"];


        }
         [self.navigationController popViewControllerAnimated:YES];
       
    }
    
    if (_PopMyBlock) {
        if (searchResults.count > 0) {
            SHBankInfoModel *info = searchResults[indexPath.row];

            _PopMyBlock(info.subBranch,info.cnapsCode );
            
        }else{
            SHBankInfoModel *info = _dataArray[indexPath.row];

            _PopMyBlock(info.subBranch,info.cnapsCode );
            
            
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    }

}

#pragma UISearchDisplayDelegate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    searchResults = [[NSMutableArray alloc]init];
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
//        
//        if (mySearchBar.text.length>0&&![ChineseInclude isIncludeChineseInString:mySearchBar.text]) {
//            
//            for (int i=0; i<_dataArray.count; i++) {
//                TDBankNameInfo * info = _dataArray[i];
//                if ([ChineseInclude isIncludeChineseInString:info.subBranch]) {
//                    NSString *tempPinYinStr = [PinYinForObjc chineseConvertToPinYin:info.subBranch];
//                    NSRange titleResult=[tempPinYinStr rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
//                    if (titleResult.length>0) {
//                        [searchResults addObject:info.subBranch];
//                    }
//                    NSString *tempPinYinHeadStr = [PinYinForObjc chineseConvertToPinYinHead:info.subBranch];
//                    NSRange titleHeadResult=[tempPinYinHeadStr rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
//                    if (titleHeadResult.length>0) {
//                        [searchResults addObject:info.subBranch];
//                    }
//                }
//                else {
//                    NSRange titleResult=[info.subBranch rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
//                    if (titleResult.length>0) {
//                        [searchResults addObject:info.subBranch];
//                    }
//                }
//            }
//        } else if (mySearchBar.text.length>0&&[ChineseInclude isIncludeChineseInString:mySearchBar.text]) {
//            for (NSString *tempStr in _dataArray) {
//                NSRange titleResult=[tempStr rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
//                if (titleResult.length>0) {
//                    [searchResults addObject:tempStr];
//                }
//            }
//        }
        if (mySearchBar.text.length>0) {
            for (int i=0; i<_dataArray.count; i++) {
                SHBankInfoModel * info = _dataArray[i];

                NSRange titleResult=[info.subBranch rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
                if (titleResult.length>0) {
                    
//                    dispatch_async(dispatch_get_main_queue(), ^{
                    
                         [searchResults addObject:info];
//                    });
                   
                }
            }
        }
  
//    });
    

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return  __I6Height(60);
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    [searchResults removeAllObjects];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    cell.frame = CGRectMake(-320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
//    [UIView animateWithDuration:0.7 animations:^{
//        cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
//    } completion:^(BOOL finished) {
//        ;
//    }];
}
@end
